import pygame
from const import (
    TURN_START_EVENT,
    Skill,
)
from typing import Tuple, Optional
from model import GridManager, Player, Model



class PowerupHandler:
    """
    管理技能道具的激活与逻辑
    """

    def __init__(self, model: Model) -> None:
        self.model = model

    @property
    def current_skill(self) -> Optional[Skill]:
        """
        当前技能类型
        """
        return self.model.selected_skill
    
    @current_skill.setter
    def current_skill(self, skill: Skill) -> None:
        """
        设置当前技能类型
        """
        self.model.selected_skill = skill

    @property
    def selected_grid_pos(self) -> Optional[Tuple[int, int]]:
        """
        当前选中的格子坐标
        """
        return self.model.selected_grid_pos
    
    @selected_grid_pos.setter
    def selected_grid_pos(self, pos: Tuple[int, int]) -> None:
        """
        设置当前选中的格子坐标
        """
        self.model.selected_grid_pos = pos

    def end_skill_state(self) -> None:
        """
        结束技能状态，重置技能处理器
        """
        self.current_skill = None
        self.selected_grid_pos = None

    def handle_skill_select(self, skill: Skill) -> None:

        if self.current_skill is not None:
            if self.current_skill == skill:
                self.end_skill_state()  # 取消技能选择
                print(f"取消技能选择：{skill}")
            else:
                print(f"请先取消当前技能选择：{self.current_skill}")

        else:

            print(f"技能选择：{skill}")

            if skill == Skill.EXPAND_GRID:
                self.model.grid_manager.expand_grid()

                pygame.event.post(pygame.event.Event(TURN_START_EVENT))  # 开始新回合

                self.model.player_manager.current_player.use_skill(skill)
            else:
                self.current_skill = skill
                
    
    
    def activate_powerup(self, model: 'Model', position: Tuple[int, int], player: 'Player') -> bool:

        powerup_type: Skill = self.current_skill

        grid_manager: GridManager = model.grid_manager

        result = False # 是否使用了技能

        if powerup_type not in player.available_skills:
            print(f"技能不可用：{powerup_type}")

            result = False
        
        # 处理多步骤技能
        if powerup_type in [Skill.MOVE_PIECE, Skill.SWAP_PIECES]:
            if self.current_skill is not None and self.current_skill != powerup_type:  # 如果是新技能则重置状态
                self.end_skill_state()
            
            if not self.selected_grid_pos:
                # 检查当前位置是否有棋子
                x, y = position
                if grid_manager.grid[y][x] is not None:
                    self.selected_grid_pos = position

                result = False

            else:
                # 执行移动/交换操作

                if powerup_type == Skill.MOVE_PIECE:
                    result = grid_manager.move_piece(self.selected_grid_pos, position)
                elif powerup_type == Skill.SWAP_PIECES:
                    result = grid_manager.swap_pieces(self.selected_grid_pos, position)
                
        
        if powerup_type == Skill.CLEAR_ROW:
            _, row = position
            grid_manager.clear_row(row)
            result = True
        
        if powerup_type == Skill.CLEAR_COLUMN:
            col, _ = position
            grid_manager.clear_column(col)
            result = True

 
        if result:
            # 使用技能后，移除技能列表中的该技能
            player.use_skill(powerup_type)

        return result




